﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// Summary description for VendorMethord
/// </summary>
public class VendorMethord
{
	public VendorMethord()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    SqlConnection sqlcon = Conection.getconnet();
    public void OpenConnect()
        { 
            if(sqlcon.State == System.Data.ConnectionState.Closed)
            {
                sqlcon.Open();
            }
        }
    public DataTable ViewAllOrder() {
        DataTable dt = new DataTable();
        SqlCommand sqlcom = new SqlCommand("ViewAllOrder",sqlcon);
        OpenConnect();
        sqlcom.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter sqlda = new SqlDataAdapter(sqlcom);
        sqlcon.Close();
        DataSet ds = new DataSet();
        sqlda.Fill(ds);
        dt = ds.Tables[0];
        return dt;
    }
    public DataTable GetUserId(int _id) {
        DataTable dt = new DataTable();
        OpenConnect();
        SqlCommand sqlcom = new SqlCommand("ViewUser",sqlcon);
        sqlcom.Parameters.Add("@OrderID",_id);
        sqlcom.CommandType = CommandType.StoredProcedure;
        sqlcom.ExecuteNonQuery();
        SqlDataAdapter sqlda = new SqlDataAdapter(sqlcom);
        DataSet ds = new DataSet();
        sqlda.Fill(ds);
        dt = ds.Tables[0];
        sqlcon.Close();
        return dt;
    }
    public DataTable VendorViewAddres(int _OrderID)
    {
        DataTable dt = new DataTable();
        OpenConnect();
        SqlCommand sqlcom = new SqlCommand("ViewAddress",sqlcon);
        sqlcom.Parameters.Add("@OrderId",_OrderID);
        sqlcom.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter sqlda = new SqlDataAdapter(sqlcom);
        sqlcon.Close();
        DataSet ds = new DataSet();
        sqlda.Fill(ds);
        dt = ds.Tables[0];
        return dt;
    }
    public DataTable VendorViewShippingType() {
        DataTable dt = new DataTable();
        OpenConnect();
        SqlCommand sqlcom = new SqlCommand("VIEWShippingType",sqlcon);
        sqlcom.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter sqlda = new SqlDataAdapter(sqlcom);
        sqlcon.Close();
        DataSet ds = new DataSet();
        sqlda.Fill(ds);
        dt = ds.Tables[0];
        return dt;
    }
    public bool AddShippingDetails(string _fulname, int PhoneNumber, int _OrderId,int _EmId,string _address, int _typeShipping, string _description) {
        bool resurt = false;
        OpenConnect();
        SqlCommand sqlcom = new SqlCommand("ShippingManager", sqlcon);
        sqlcom.Parameters.Add("@FullName", _fulname);
        sqlcom.Parameters.Add("@PhoneNumber", PhoneNumber);
        sqlcom.Parameters.Add("@OrderId", _OrderId);
        sqlcom.Parameters.Add("@EmployeeID", _EmId);
        sqlcom.Parameters.Add("@Address", _address);
        sqlcom.Parameters.Add("@TypeOfShipping", _typeShipping);
        sqlcom.Parameters.Add("@Description", _description);
        sqlcom.CommandType = CommandType.StoredProcedure;
        try
        {
            sqlcom.ExecuteNonQuery();
            resurt = true;
            sqlcon.Close();
        }
        catch (Exception ex)
        {

            sqlcon.Close();
            resurt = false;
        }
        return resurt;
     }
    public bool VendorUpdateOrder(int _id) {
        bool res = false;
        OpenConnect();
        SqlCommand sqlcom = new SqlCommand("VendorChangeOrder",sqlcon);
        sqlcom.Parameters.Add("@OrderId",_id);
        sqlcom.CommandType = CommandType.StoredProcedure;
        try
        {
            sqlcom.ExecuteNonQuery();
            res = true;
        }
        catch (Exception ex)
        {
            res = false;
            sqlcon.Close();
            
        }
        return res;
    }
    public DataTable LoadCatelogy() {
        DataTable dt = new DataTable();
        OpenConnect();
        SqlCommand sqlcom = new SqlCommand("LoadCatelogy",sqlcon);
        sqlcom.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter sqda = new SqlDataAdapter(sqlcom);
        sqlcon.Close();
        DataSet ds = new DataSet();
        sqda.Fill(ds);
        dt = ds.Tables[0];
        return dt;
    }
    public bool AddnewProduct(string Proname,int CatId,int Quantity,string Imager,float width,float weight,float lenght,float price,string description) {
        bool resurt;
        SqlCommand sqlcom = new SqlCommand("AddnewProduct",sqlcon);
        sqlcom.Parameters.Add("@ProductName", Proname);
        sqlcom.Parameters.Add("@CatID", CatId);
        sqlcom.Parameters.Add("@Quantity", Quantity);
        sqlcom.Parameters.Add("@UrlImage", Imager);
        sqlcom.Parameters.Add("@Width",width);
        sqlcom.Parameters.Add("@Weight", weight);
        sqlcom.Parameters.Add("@Lenght", lenght);
        sqlcom.Parameters.Add("@Price", price);
        sqlcom.Parameters.Add("@Description", description);
        sqlcom.CommandType = CommandType.StoredProcedure;
        try
        {
            OpenConnect();
            sqlcom.ExecuteNonQuery();
            resurt = true;
            sqlcon.Close();
        }
        catch (Exception)
        {
            resurt = false;
            sqlcon.Close();
            
        }
        return  resurt;
    }
    public DataTable VendorViewUser(int UserId) {
        DataTable dt = new DataTable();
        SqlCommand sqlcom = new SqlCommand("VendorViewUser", sqlcon);
        sqlcom.Parameters.Add("@UserID",UserId);
        sqlcom.CommandType = CommandType.StoredProcedure;
        OpenConnect();
        SqlDataAdapter sqlda = new SqlDataAdapter(sqlcom);
        sqlcon.Close();
        DataSet ds = new DataSet();
        sqlda.Fill(ds);
        dt = ds.Tables[0];
        return dt;
    }
    public DataTable VendorSearchByOrderID(int OrderId)
    {
        DataTable dt = new DataTable();
        SqlCommand sqlcom = new SqlCommand("VendorSearchByOrderID", sqlcon);
        sqlcom.Parameters.Add("@OrderID",OrderId);
        sqlcom.CommandType = CommandType.StoredProcedure;
        OpenConnect();
        SqlDataAdapter sqlda = new SqlDataAdapter(sqlcom);
        sqlcon.Close();
        DataSet ds = new DataSet();
        sqlda.Fill(ds);
        dt = ds.Tables[0];
        return dt;
    }

    public DataTable VendorSearchByUserID(int ID) {
        DataTable dt = new DataTable();
        SqlCommand sqlcom = new SqlCommand("VendorSearchByUserID",sqlcon);
        sqlcom.Parameters.Add("@UserID",ID);
        sqlcom.CommandType = CommandType.StoredProcedure;
        OpenConnect();
        sqlcom.ExecuteNonQuery();
        SqlDataAdapter sqlda = new SqlDataAdapter(sqlcom);
        DataSet ds = new DataSet();
        sqlda.Fill(ds);
        dt = ds.Tables[0];
        return dt;
    }
    public DataTable VendorViewOrderDetail(int orderId)
    {
        DataTable dt = new DataTable();
        OpenConnect();
        SqlCommand sqlcom = new SqlCommand("VendorViewOrderDetail", sqlcon);
        sqlcom.Parameters.Add("@OrderID", orderId);
        sqlcom.CommandType = CommandType.StoredProcedure;
        
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
        SqlDataAdapter sqlda = new SqlDataAdapter(sqlcom);
        DataSet ds = new DataSet();
        sqlda.Fill(ds);
        dt = ds.Tables[0];
        return dt;
    }
    public DataTable ViewProductInOrderDetails(int Id) {
        DataTable dt = new DataTable();
        OpenConnect();
        SqlCommand sqlcom = new SqlCommand("ProductInOrderDetail", sqlcon);
        sqlcom.Parameters.Add("@OrderId", Id);
        sqlcom.CommandType = CommandType.StoredProcedure;

        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
        SqlDataAdapter sqlda = new SqlDataAdapter(sqlcom);
        DataSet ds = new DataSet();
        sqlda.Fill(ds);
        dt = ds.Tables[0];
        return dt;
    }
    public DataTable VendorViewconsgnee(int id) {
        DataTable dt = new DataTable();
        SqlCommand sqlcom = new SqlCommand("ViewConsigneeInformation",sqlcon);
        OpenConnect();
        sqlcom.Parameters.Add("@OrderId",id);
        sqlcom.CommandType = CommandType.StoredProcedure;
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
        SqlDataAdapter sqlda = new SqlDataAdapter(sqlcom);
        DataSet ds = new DataSet();
        sqlda.Fill(ds);
        dt = ds.Tables[0];
        return dt;
    }
    public DataTable VendorViewProduct() {
        DataTable dt = new DataTable();
        SqlCommand sqlcom = new SqlCommand("VendorViewAllProduct", sqlcon);
        OpenConnect();        
        sqlcom.CommandType = CommandType.StoredProcedure;
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
        SqlDataAdapter sqlda = new SqlDataAdapter(sqlcom);
        DataSet ds = new DataSet();
        sqlda.Fill(ds);
        dt = ds.Tables[0];
        return dt;
    }
    public DataTable VendorSearchbyName(string Name) {
        DataTable dt = new DataTable();
        SqlCommand sqlcom = new SqlCommand("VendorSearchProductByName",sqlcon);
        sqlcom.Parameters.Add("@ProductName", Name);
        sqlcom.CommandType = CommandType.StoredProcedure;
        OpenConnect();
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
        SqlDataAdapter sqlda = new SqlDataAdapter(sqlcom);
        DataSet ds = new DataSet();
        sqlda.Fill(ds);
        dt = ds.Tables[0];
        return dt;
    }
    public DataTable VendorSearchbyId(int Id) {
        DataTable dt = new DataTable();
        SqlCommand sqlcom = new SqlCommand("VendorSearchProductByID", sqlcon);
        sqlcom.Parameters.Add("@ProductID", Id);
        sqlcom.CommandType = CommandType.StoredProcedure;
        OpenConnect();
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
        SqlDataAdapter sqlda = new SqlDataAdapter(sqlcom);
        DataSet ds = new DataSet();
        sqlda.Fill(ds);
        dt = ds.Tables[0];
        return dt;
    }
    public DataTable Login(string username, string pass) {
        DataTable dt = new DataTable();
        SqlCommand sqlcom = new SqlCommand("LoginManagerVendor", sqlcon);
        sqlcom.Parameters.Add("@UserName",username);
        sqlcom.Parameters.Add("@Password",pass);
        sqlcom.CommandType = CommandType.StoredProcedure;
        OpenConnect();
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
        SqlDataAdapter sqlda = new SqlDataAdapter(sqlcom);
        DataSet ds = new DataSet();
        sqlda.Fill(ds);
        dt = ds.Tables[0];
        return dt;
    }
    public DataTable getIDemploy(string username) {
        DataTable dt = new DataTable();
        SqlCommand sqlcom = new SqlCommand("GetIdEmployee", sqlcon);
        sqlcom.Parameters.Add("@UserName", username);        
        sqlcom.CommandType = CommandType.StoredProcedure;
        OpenConnect();
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
        SqlDataAdapter sqlda = new SqlDataAdapter(sqlcom);
        DataSet ds = new DataSet();
        sqlda.Fill(ds);
        dt = ds.Tables[0];
        return dt;
    }
    public bool ChangePasswordManager(int ID, string newpass) {
        bool resrt = false;
        SqlCommand sqlcom = new SqlCommand("ChangePass",sqlcon);
        sqlcom.Parameters.Add("@EmpID",ID);
        sqlcom.Parameters.Add("@NewPass",newpass);
        sqlcom.CommandType = CommandType.StoredProcedure;

        try
        {
            OpenConnect();
            sqlcom.ExecuteNonQuery();
            resrt = true;
            sqlcon.Close();
        }
        catch (Exception)
        {
            resrt = false;
            sqlcon.Close();
            throw;
        }
        return resrt;
    
    }
    public DataTable ViewInformation(string user) {
        DataTable dt = new DataTable();
        SqlCommand sqlcom = new SqlCommand("ViewVendorInfomation", sqlcon);
        sqlcom.Parameters.Add("@UserName", user);
        sqlcom.CommandType = CommandType.StoredProcedure;
        OpenConnect();
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
        SqlDataAdapter sqlda = new SqlDataAdapter(sqlcom);
        DataSet ds = new DataSet();
        sqlda.Fill(ds);
        dt = ds.Tables[0];
        return dt;
    }
}